package com.hl.sfzy.utils;

import com.hl.sfzy.constant.ResultCode;
import org.springframework.http.HttpStatus;
import lombok.Getter;

import java.io.Serializable;

@Getter
/**
 * 全局统一相应对象
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private long code;
    private String message;
    private T data;
    private String timestamp;

    private R() {
        this.timestamp = String.valueOf(System.currentTimeMillis());
    }

    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> success(T data) {
        return new R<T>()
                .code(ResultCode.SUCCESS.getCode())
                .message("操作成功")
                .data(data);
    }

    public static <T> R<T> failure(T data) {
        return new R<T>()
                .code(ResultCode.Fail.getCode())
                .message("操作失败")
                .data(data);
    }

    public static <T> R<T> failure(String message) {
        return failure(ResultCode.Fail.getCode(), message);
    }

    public static <T> R<T> failure(long code, String message) {
        return new R<T>().code(code).message(message);
    }

    public R<T> code(long code) {
        this.code = code;
        return this;
    }

    public R<T> message(String message) {
        this.message = message;
        return this;
    }

    public R<T> data(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "R{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", data=" + data +
                ", timestamp='" + timestamp + '\'' +
                '}';
    }
}